#include <bits/stdc++.h>
using namespace std;

int GCD(int a, int b) {
  int c = a % b;

  while (c) {
    a = b;
    b = c;
    c = a % b;
  }
  return b;
}

bool Check(const vector<int>& prefix, const vector<int>& suffix) {
  int n = prefix.size();
  if (prefix.back() != suffix.front()) {
    return false;
  }
  for (int i = 1; i < n; i++) {
    if (prefix[i - 1] % prefix[i] != 0) {
      return false;
    }
    if (suffix[i] % suffix[i - 1] != 0) {
      return false;
    }
  }
  for (int i = 0; i + 1 < n; i++) {
    if (GCD(prefix[i], suffix[i + 1]) != suffix.front()) {
      return false;
    }
  }
  return true;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int T;
  for (cin >> T; T; T--) {
    int n;

    cin >> n;

    vector<int> prefix(n), suffix(n);

    for (int& x : prefix) {
      cin >> x;
    }
    for (int& x : suffix) {
      cin >> x;
    }
    cout << (Check(prefix, suffix) ? "YES\n" : "NO\n");
  }
  return 0;
}